<?php

namespace app\common\model;

use think\Request;
use think\Model;

class AppletUser extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    protected $append = [
        'usable'
    ];

    /**
     * 计算可用积分
     */
    public function getUsableAttr($value,$data)
    {
        return $data['score'] - $data['converted'];
    }

    /**
     * 显示性别
     */
    public function getGenderAttr($value)
    {
        $status = [0 => '未知', 1 => '先生', 2 => '女士'];
        return $status[$value];
    }

    /**
     * 关联收货地址表
     */
    public function address()
    {
        return $this->hasMany('app\\common\\model\\Address', 'applet_user_id','id');
    }

    /**
     * 关联收货地址表 (默认地址)
     */
    public function addressDefault()
    {
        return $this->belongsTo('app\\common\\model\\Address', 'address_id','id');
    }

    /**
     * 获取列表
     * $mode 获取模式app=小程序获取
     */
    public function getList($applet_id=0,$search='')
    {
        // 筛选条件
        $filter = [];
        $applet_id > 0 && $filter['applet_id'] = $applet_id;
        if(!empty($search)){
            //是否是数字
            if(is_numeric($search)){   
                //是否是手机号
                if(isMobile($search)){
                    $filter['mobile'] = $search;
                }else{
                    $filter['id'] = $search;
                }
            }else{
               //不是数字   
                $filter['nickname'] = ['like', '%' . trim($search) . '%'];
            }
        }
        // 执行查询
        $list = $this->with(['address','addressDefault'])
            ->where($filter)
            ->order('id','desc')
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
        return $list;
    }

    /**
     * 获取详情
     */
    public static function getOpenid($open_id,$applet_id=0)
    {
        // 筛选条件
        $filter['open_id'] = $open_id;
        $applet_id > 0 && $filter['applet_id'] = $applet_id;
        return self::where($filter)->find();
    }

    /**
     * 获取用户信息
     */
    public static function detail($where)
    {
        return self::get($where, ['address', 'addressDefault']);
    }

    /**
     * 修改
     */
    public function edit($data)
    {
        switch ($data['gender']) {
            case '先生':
                $data['gender'] = 1;
                break;
            case '女士':
                $data['gender'] = 2;
                break;
            default:
                $data['gender'] = 0;
                break;
        }
        return $this->allowField(true)->save($data);
    }

    /**
     * 根据条件统计数量
     */
    public static function getCount($fo_id)
    {
        $self = new static;
         // 筛选条件
        $filter = [];
        $filter['formwork_order_id'] = $fo_id;
        $count = array();
        //全部
        $count[0] = self::where($filter)->count();
        //今天
        $star = strtotime(date("Y-m-d"),time());
        $count[1] = self::where($filter)->where('createtime','>',$star)->count();
        //昨天
        $star = strtotime("-1 day");
        $end = strtotime(date("Y-m-d"),time());
        $count[2] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //前天
        $star = strtotime("-2 day");
        $end = strtotime("-1 day");
        $count[3] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //-4天
        $star = strtotime("-3 day");
        $end = strtotime("-2 day");
        $count[4] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //-5天
        $star = strtotime("-4 day");
        $end = strtotime("-3 day");
        $count[5] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //-6天
        $star = strtotime("-5 day");
        $end = strtotime("-4 day");
        $count[6] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //-7天
        $star = strtotime("-6 day");
        $end = strtotime("-5 day");
        $count[7] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        //本月起至时间 - 月度统计 
        $end = mktime(0,0,0,date('m'),1,date('y')); 
        $count[8] = self::where($filter)->where('createtime','>',$end)->count();
        
        //上月开始  
        $star = mktime(0,0,0,date('m')-1,1,date('y')); 
        $count[9] = self::where($filter)->where('createtime','>',$star)->where('createtime','<',$end)->count();
        
        return $count;
    }
}
